<?php
$page_title = "Sondaj - Fifaonline.ro | Campionate fifa virtuale | Stiri sportive";
include_once("top.php");
?>
<?php
require_once ("/includes/mysql_connect.php"); //Realizeaza conexiunea la baza de date.
if(isset($_GET['groupname'])){
	$group_name = $_GET['groupname'];
	$request = "SELECT * FROM polls WHERE group_name ='" .$group_name. "'";
	$result = mysql_query($request);
	if(mysql_num_rows($result) == 0){ 
		$group_name = FALSE;
		$alert = "Acest id nu exista";
	}
	else{
		$alert = FALSE;
	}
}
else{
	$group_name = FALSE;
}
if(isset($_POST['submitted'])){
	if(isset($_POST[$group_name])){
		require_once("/includes/mysql_connect.php");
		$request = "SELECT votes FROM polls WHERE group_name='".$group_name."'AND data='".$group_name."'";
		$result = mysql_query($request);
		$row = mysql_fetch_array($result);
		$field = mysql_fetch_row($result);
		$voturi = $row['votes'];
		$nvoturi = $voturi + 1;
		$request = "UPDATE polls SET votes = $nvoturi WHERE group_name = '" . $group_name . "' AND data = '" .$_POST[$group_name]. "'";
		$result = mysql_query($request);
		$query = "SELECT data FROM polls WHERE group_name = '" . $group_name . "' AND type = 'a'";
		$result = mysql_query($query);
		$num_rows = mysql_num_rows($result);
		$query = "SELECT SUM(votes) FROM polls WHERE group_name='" .$group_name. "' AND type = 'a'";
		$result = mysql_query($query);
		while($row = mysql_fetch_array($result)){
			$q = $row['SUM(votes)'];
		}
		$query = "UPDATE data SET votes = $q WHERE group_name = '" .$group_name. "' AND type = 'q'";
		$result = mysql_query($query);
		$query = "SELECT votes FROM polls WHERE group_name = '" .$group_name. "' AND type = 'a'";
		$result = mysql_query($query);
		$num_rows = mysql_num_rows($result);
		for($i = 1; $i <= $num_rows; $i++){
			$query = "SELECT votes FROM polls WHERE group_name = '" .$group_name. "' AND a_id = $i";
			$result = mysql_query($query);
			$row = mysql_fetch_array($result);
			$a[$i] = $row['votes'];
			$x[$i] = ($a[$i] * 100)/$q;
			$query = "UPDATE polls SET procent = $x[$i] WHERE group_name = '" .$group_name. "' AND a_id = $i";
			$result = mysql_query($query);
		}
		$_SESSION[$group_name] = $group_name ;
	}
}
?>
<script type="text/javascript">

function enableElement()
{
document.getElementById("sbmt").disabled=false;
}

</script>
<?php
if(!isset($_SESSION[$group_name])){
		$request = "SELECT data FROM polls WHERE group_name ='" .$group_name. "'";
		$result2 = @mysql_query($request);
		$row = mysql_fetch_array($result2);
		echo "<p class = 'text3'>" .$row['data']. "</p>";
		$request6 = "SELECT data FROM polls WHERE group_name ='" .$group_name. "' AND type = 'a'";
		$result6 = mysql_query($request6);
		echo "<form name = '".$group_name."' action='pollbygroupname.php?groupname=".$group_name."' method='post'/>";
		while ($row = mysql_fetch_array($result6)){
			echo "<p class = 'text4'><input type = 'radio' onchange = 'enableElement()' name = '". $group_name . "' value = '".$row['data']."'> ".$row['data']. "</p><br>";
		}
		echo "<input type = 'submit' name = 'submit' value = 'Vote!' disabled='disabled' id='sbmt'/>";
		echo "<input type=\"hidden\" name=\"submitted\" value=\"TRUE\"  />";
		echo "</form>";
		$query = "SELECT votes FROM polls WHERE group_name = '" .$group_name. "' AND type = 'q'";
		$result = mysql_query($query);
		while($row = mysql_fetch_array($result)){
			$q = $row['votes'];
			echo "" . $q . "votes";
	}
}
else {
echo "<b>";
	echo $_SESSION[$group_name];
	echo "</b>";
	if($group_name) {
		$request = "SELECT data FROM polls WHERE group_name ='" .$group_name. "'";
		$result2 = @mysql_query($request);
		$row = mysql_fetch_array($result2);
		echo "<p class = 'text3'><u>" .$row['data']. "</u></p><br>";
		$request6 = "SELECT data FROM polls WHERE group_name ='" .$group_name. "' AND type = 'a'";
		$result6 = mysql_query($request6);
		$query = "SELECT a_id, procent, data FROM polls WHERE group_name='" .$group_name ."' AND type='a'";
		$result = mysql_query($query);
		while ($row = mysql_fetch_array($result)){
			$a_id = $row['a_id'];
			$width = $row['procent'] * 2;
			echo "<p class = 'text4'>".$row['data']." (";
			echo $width/2;
			echo "%) </p><br>";
			echo "<p><img src='/images/bar".$a_id.".png' height='25' width='$width'></p><br>";
		}
		$query = "SELECT votes FROM polls WHERE group_name = '" .$group_name. "' AND type = 'q'";
		$result = mysql_query($query);
		while($row = mysql_fetch_array($result)){
			$q = $row['votes'];
			echo "" . $q . " votes";
		}
	}
}
include_once("end.php");
?>